<template>
  <div class="todo">
    <el-carousel
      :interval="4000"
      type="card"
      height="200px"
      arrow="always"
    >
      <el-carousel-item
        v-for="item in 6"
        :key="item">
        <h3 class="medium">
          {{ item }}
        </h3>
      </el-carousel-item>
    </el-carousel>
    <create-todo @item-data="getItemData"/>
    <todo-items
      v-loading.fullscreen.lock="isLoading"
      :list="list"
    />
  </div>
</template>

<script>
import http from "axios";
import CreateTodo from '@/components/CreateTodo'
import TodoItems from '@/components/TodoItems'
// import { Carousel, Slide } from 'vue-carousel'

export default {
  name: "TodoList",
  components: {
    CreateTodo,
    TodoItems,
    // Carousel,
    // Slide
  },
  data() {
    return {
      list: [],
      isLoading: false
    }
  },
  created() {
    this.init()
  },
  methods: {
    // nodejs php java
    // 假数据
    // database
    init() {
      this.isLoading = true
      http({
        // url: 'https://api.yizhanketang.cn/api/v1/todos'
        url: 'http://localhost:3000/todos'
      }).then((res) => {
        this.list = res.data
      }).catch(() => {
        this.$message({
          message: '网络错误，请刷新重试',
          type: 'error'
        });
      }).finally(() => {
        this.isLoading = false
      })
    },
    getItemData(option) {
      if(option.content && option.content.length > 0) {
        http({
          method: 'POST',
          url: 'http://localhost:3000/todos',
          data: option
        }).then(() => {
          this.init()
        })
      } else {
        alert('不能为空')
      }
    }
  }
}
</script>

<style lang="scss">

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>